package ncku.kdd.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * You can load a outside property file by using this class
 * @author ktchuang
 *
 */
public class PropertyLoader {
	private static GlobalProperties gprop = null;
	/*load a property*/
    public static Properties load(Propertyable obj, String prop_fn) {
    	if (gprop == null)
    		loadGlobalProperties();
    	boolean exists = (new File(prop_fn)).exists();    	
    	Properties prop = null;
		if (exists) {
		    prop = new Properties();
		    try {
				prop.load(new FileInputStream(prop_fn));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			if (obj != null)
              prop = obj.loadDefaultProperties();
		}
		return prop;    	
    }
    private static void loadGlobalProperties() {
    	Properties prop = load(null, GlobalProperties.GLOBAL_PROP_FILE);
    	GlobalProperties.DB_PROP_FILE = prop.getProperty("DB_PROP_FILE");
    	
    }
    
    
}
